package cn.iocoder.yudao.module.product.service.calculationmodel;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.product.controller.admin.calculationmodel.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.calculationmodel.CalculationModelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.product.convert.calculationmodel.CalculationModelConvert;
import cn.iocoder.yudao.module.product.dal.mysql.calculationmodel.CalculationModelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 核算模型 Service 实现类
 *
 * @author wcl
 */
@Service
@Validated
public class CalculationModelServiceImpl implements CalculationModelService {

    @Resource
    private CalculationModelMapper calculationModelMapper;

    @Override
    public Long createCalculationModel(CalculationModelCreateReqVO createReqVO) {
        // 插入
        CalculationModelDO calculationModel = CalculationModelConvert.INSTANCE.convert(createReqVO);
        calculationModelMapper.insert(calculationModel);
        // 返回
        return calculationModel.getId();
    }

    @Override
    public void updateCalculationModel(CalculationModelUpdateReqVO updateReqVO) {
        // 校验存在
        validateCalculationModelExists(updateReqVO.getId());
        // 更新
        CalculationModelDO updateObj = CalculationModelConvert.INSTANCE.convert(updateReqVO);
        calculationModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteCalculationModel(Long id) {
        // 校验存在
        validateCalculationModelExists(id);
        // 删除
        calculationModelMapper.deleteById(id);
    }

    private void validateCalculationModelExists(Long id) {
        if (calculationModelMapper.selectById(id) == null) {
            throw exception(CALCULATION_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public CalculationModelDO getCalculationModel(Long id) {
        return calculationModelMapper.selectById(id);
    }

    @Override
    public List<CalculationModelDO> getCalculationModelList(Collection<Long> ids) {
        return calculationModelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CalculationModelDO> getCalculationModelPage(CalculationModelPageReqVO pageReqVO) {
        return calculationModelMapper.selectPage(pageReqVO);
    }

}
